package com.mspbots.web.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author sean.wang
 * @date Created by 2020/02/27
 */
@Configuration
public class RabbitConfiguration {

    public static final String EXCHANGE_NAME = "msp.reports";
    public static final String LOGINS_RECORD_QUEUE = "report.message.LoginsRecord";
    public static final String PAGEVIEWS_QUEUE = "report.message.Pageviews";

    public static final String EXCHANGE_MSP_TENANT_SYNC = "msp.tenant.sync";
    public static final String TENANT_SYNC_INFO = "tenant.sync.info";

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Queue loginsRecordQueue() {
        return new Queue(LOGINS_RECORD_QUEUE);
    }

    @Bean
    public Queue pageviewsQueue() {
        return new Queue(PAGEVIEWS_QUEUE);
    }

    @Bean
    public Binding bindingLoginsRecordQueue() {
        return BindingBuilder.bind(loginsRecordQueue()).to(topicExchange()).with(LOGINS_RECORD_QUEUE);
    }

    @Bean
    public Binding bindingPageviewsQueue() {
        return BindingBuilder.bind(pageviewsQueue()).to(topicExchange()).with(PAGEVIEWS_QUEUE);
    }


    @Bean
    public DirectExchange tenantExchange() {
        return new DirectExchange(EXCHANGE_MSP_TENANT_SYNC);
    }

    @Bean
    public Queue tenantSyncInfoQueue() {
        return new Queue(TENANT_SYNC_INFO);
    }

    @Bean
    public Binding bindingTenantSyncInfoQueue() {
        return BindingBuilder.bind(tenantSyncInfoQueue()).to(tenantExchange()).withQueueName();
    }


    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }
}
